package app

import (
	"chatgpt/controller/father"
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/logq"
	"chatgpt/plugin/sms"
	"chatgpt/services"
	"github.com/gin-gonic/gin"
	"github.com/slclub/go-tips"
	"net/http"
)

func RegisterWithAccountHandle(ctx *gin.Context) {
	newuser := services.UserSrv.NewUser()
	newuser.ConvFromMap(father.Raw(ctx))
	if mc := father.UserRegisterCheck(ctx, newuser); mc != code.SUCCESS {
		helper.Count(ctx, mc, nil)
		return
	}
	if mc := services.UserSrv.CheckAccount(newuser); mc != code.SUCCESS {
		helper.Count(ctx, mc, nil)
		return
	}
	//newuser.Passwd = helper.LoginAccount.AccountPassword(newuser.Passwd)
	mc := services.UserSrv.Create(newuser)
	rtn := newuser.ConvToMap()
	rtn["Token"] = services.UserSrv.Login(newuser)
	helper.Count(ctx, mc, newuser.ConvToMap())
	services.RegisterUserSrv.EventAction(newuser)
	logq.InfoF("HANDLE.LOGIN.REGSTER-WITH-ACCOUNT %v", *newuser)
}

func LoginWithAccountHandle(ctx *gin.Context) {
	login := father.RawString(ctx, "Login")
	passwd := father.RawString(ctx, "Passwd")

	usr := services.UserSrv.GetWithAccountDB(login)
	if usr == nil {
		helper.Count(ctx, code.NON_REGISTER_ERR, nil)
		return
	}

	if mc := helper.LoginAccount.PasswdCheck(usr, passwd, "account"); mc != code.SUCCESS {
		helper.Echo(ctx, mc, nil)
		return
	}
	rtn := usr.ConvToMap()
	token := services.UserSrv.Login(usr)
	rtn["Token"] = token
	helper.Count(ctx, code.SUCCESS, rtn)
	logq.InfoF("HANDLE.LOGIN.LOGIN-WITH-ACCOUNT %v", rtn)
}

func UpdateHandle(ctx *gin.Context) {
	newuser := services.UserSrv.NewUser()
	newuser.ConvFromMap(father.Raw(ctx))
	//if mc := father.UserRegisterCheck(ctx, newuser); mc != code.SUCCESS {
	//	helper.Count(ctx, mc, nil)
	//	return
	//}
	//newuser.Passwd = helper.LoginAccount.AccountPassword(newuser.Passwd)
	//if newuser.NickName == "" {
	//	newuser.NickName = newuser.Cell
	//}
	mc := services.UserSrv.Create(newuser)
	rtn := newuser.ConvToMap()
	rtn["Token"] = services.UserSrv.Login(newuser)
	helper.Count(ctx, mc, newuser.ConvToMap())
	logq.InfoF("HANDLE.LOGIN.REGSTER-WITH-CELL %v", *newuser)
	helper.Count(ctx, code.SUCCESS, nil)
	logq.InfoF("HANDLE.LOGIN.UPDATE %v", nil)
}

// 个人中心
func MineCenterHandle(ctx *gin.Context) {
	uuid := ctx.GetString("UUID")
	usr := services.UserSrv.GetUserWithUUID(uuid)
	if usr == nil {
		helper.Echo(ctx, code.LOGIN_ID_PASS_ERR, nil)
		return
	}
	rtn := map[string]any{
		"User": usr.ConvToMap(),
		"Benefits": map[string]any{
			"VipExpireAt": services.BenefitSrv.BenefitUser.GetExpire(usr.UUID),
		},
	}
	helper.Echo(ctx, int(code.SUCCESS), rtn)
}

// 获取手机号
func CellGetVerifyCode(ctx *gin.Context) {

	cell := father.RawString(ctx, "Cell")

	if !helper.VerifyMobileFormatAny(cell) {
		helper.Echo(ctx, code.VERIDU_CODE_CELL_ERR, nil)
		return
	}
	if helper.CheckWhiteCellWithDebug(cell) {
		ctx.JSON(http.StatusOK, code.SUCCESS)
		return
	}

	err := sms.GetInstance().GetVerifyCode(tips.Any2Int64(cell))
	if err != nil {
		helper.Echo(ctx, code.VERIDY_CODE_MAX_LIMIT, err.Error())
		return
	}

	helper.Echo(ctx, code.SUCCESS, nil)
	logq.InfoF("LOGIN.CHECK_CODE.GET.END msg[%v]", cell)
}

func LoginWithCellHandle(ctx *gin.Context) {
	m := struct {
		Cell       string
		VerifyCode string
	}{
		Cell:       father.RawString(ctx, "Cell"),
		VerifyCode: father.RawString(ctx, "Code"),
	}
	cnf := helper.Config().Sub("SMS")
	logq.InfoF("LOGIN.LoginWithCellHandle.START :: cell [%v] v-code [%v]", m.Cell, m.VerifyCode)
	//Debug状态
	if !helper.CheckWhiteCellWithDebug(m.Cell) {
		// 校验手机号码格式
		if !helper.VerifyMobileFormatAny(m.Cell) {
			helper.Echo(ctx, code.VERIDU_CODE_CELL_ERR, nil)
			return
		}

		// 校验验证码格式
		if len(m.VerifyCode) != cnf.GetInt("code_length") {
			helper.Echo(ctx, code.VERIFY_CODE_ERR, nil)
			return
		}
		logq.DebugF("LOGIN.CheckVerifyCode [CODE:%v]", m.VerifyCode)
		if !sms.GetInstance().CheckVerifyCode(tips.Any2Int64(m.Cell), m.VerifyCode) {
			helper.Echo(ctx, code.VERIFY_CODE_ERR, nil)
			return
		}
	}

	usr := services.UserSrv.GetWithFieldDB("Cell", m.Cell)
	if usr == nil {
		helper.Echo(ctx, code.LOGIN_NEED_REGISTER, nil)
		return
	}
	rtn := usr.ConvToMap()
	token := services.UserSrv.Login(usr)
	rtn["Token"] = token
	helper.Count(ctx, code.SUCCESS, rtn)
	logq.InfoF("LOGIN.LoginWithCellHandle.END :: cell [%v] token [%v] name:%v", m.Cell, token, usr.NickName)
}

func RegisterWithCellHandle(ctx *gin.Context) {
	newuser := services.UserSrv.NewUser()
	newuser.ConvFromMap(father.Raw(ctx))
	//if mc := father.UserRegisterCheck(ctx, newuser); mc != code.SUCCESS {
	//	helper.Count(ctx, mc, nil)
	//	return
	//}
	//newuser.Passwd = helper.LoginAccount.AccountPassword(newuser.Passwd)
	usr := services.UserSrv.GetWithFieldDB("Cell", newuser.Cell)
	if usr != nil {
		helper.Echo(ctx, code.NON_CELL_REPEAT, nil)
		return
	}
	if newuser.NickName == "" {
		newuser.NickName = newuser.Cell
	}
	mc := services.UserSrv.Save(newuser)
	rtn := newuser.ConvToMap()
	rtn["Token"] = services.UserSrv.Login(newuser)
	helper.Count(ctx, mc, newuser.ConvToMap())
	services.RegisterUserSrv.EventAction(newuser)
	logq.InfoF("HANDLE.LOGIN.REGSTER-WITH-CELL %v", *newuser)
}

func CheckToken(ctx *gin.Context) {
	helper.Count(ctx, code.SUCCESS, nil)
}
